<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  String _id=request.getParameter("_");
%>

<div class="row">
    <div class="col-lg-12">
        <div class="panel">
            <div class="panel-heading">
                <h3 class="panel-title">任务发起登记表</h3>
            </div>

            <!-- BASIC FORM ELEMENTS -->
            <!--===================================================-->
            <form id="form_task_input" class="panel-body form-horizontal form-padding">
                <input type="hidden" name="keyId" id="keyId" value="${task.id}" />

                <div class="form-group">
                    <label class="col-md-2 control-label">所属项目<i class="fa fa-asterisk txt-color-red"></i></label>
                    <div class="col-md-6">
                        <select id="dictItemId" name="dictItemId" class="form-control">
                            <option></option>
                            <c:forEach var="item" items="${dictItems}" varStatus="status">
                                <option value="${item.id}" <c:if test="${item.id == task.dictItem.id}"> selected="selected"</c:if>>${item.name}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>

                <c:if test="${task.numStatus==0 || handle=='revise'}">
                    <div class="form-group">
                        <label class="col-md-2 control-label">
                            <a href="javascript:void(0);" onclick="fn_tasks_dialog_select('relateTaskNames','relateTaskIds')" style="color: #0a6dbd">关联任务</a>
                        </label>
                        <div class="col-md-6 input-group mar-btm">
                            <input type="text" id="relateTaskNames" name="relateTaskNames" class="form-control" disabled
                                   value="<c:forEach var="item" items="${task.relateTasks}" varStatus="status">${item.name}<c:if test="${!status.last}">,</c:if></c:forEach>">
                            <span class="input-group-btn">
                                <button onclick="clearRelateTasks('relateTaskNames','relateTaskIds')" class="btn btn-mint" type="button"><i class="fa fa-close"></i></button>
                            </span>
                            <input type="hidden" id="relateTaskIds" name="relateTaskIds"
                                   value="<c:forEach var="item" items="${task.relateTasks}" varStatus="status">${item.id}<c:if test="${!status.last}">,</c:if></c:forEach>">
                        </div>
                    </div>
                </c:if>

                <div class="form-group">
                    <label class="col-md-2 control-label">任务名称<i class="fa fa-asterisk txt-color-red"></i></label>
                    <div class="col-md-6">
                        <input type="text" id="name" name="name" class="form-control" placeholder="请输入任务名称" value="${task.name}">
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-2 control-label">详细要求<i class="fa fa-asterisk txt-color-red"></i></label>
                    <div class="col-md-6">
                        <input type="hidden" id="detail" name="detail" class="form-control" value="${task.detail}">
                        <div id="detail-summernote">

                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-2 control-label">
                        <a href="javascript:void(0);" onclick="fn_members_dialog_select('leaderNames','leaderIds')" style="color: #0a6dbd">任务责任人</a>
                        <i class="fa fa-asterisk txt-color-red"></i>
                    </label>
                    <div class="col-md-6">
                        <input type="text" id="leaderNames" name="leaderNames" class="form-control" placeholder="请选择任务责任人"
                               value="<c:forEach var="item" items="${task.leaders}" varStatus="status">${item.name}<c:if test="${!status.last}">,</c:if></c:forEach>">
                        <input type="hidden" id="leaderIds" name="leaderIds"
                               value="<c:forEach var="item" items="${task.leaders}" varStatus="status">${item.id}<c:if test="${!status.last}">,</c:if></c:forEach>">
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-2 control-label">
                        <a href="javascript:void(0);" onclick="fn_members_dialog_select('memberNames','memberIds')" style="color: #0a6dbd">任务组员</a>
                        <i class="fa fa-asterisk txt-color-red"></i>
                    </label>
                    <div class="col-md-6">
                        <input type="text" id="memberNames" name="memberNames" class="form-control" placeholder="请选择任务组员"
                               value="<c:forEach var="item" items="${task.members}" varStatus="status">${item.name}<c:if test="${!status.last}">,</c:if></c:forEach>">
                        <input type="hidden" id="memberIds" name="memberIds"
                               value="<c:forEach var="item" items="${task.members}" varStatus="status">${item.id}<c:if test="${!status.last}">,</c:if></c:forEach>">
                    </div>
                </div>

                <div class="form-group div-Hidden">
                    <label class="col-md-2 control-label">计划开始时间<i class="fa fa-asterisk txt-color-red"></i></label>
                    <div class="col-md-6">
                            <div class="input-group date">
                                <input type="text" class="form-control" id="datPlanStart" name="datPlanStart" placeholder="请选择计划开始时间"
                                value=<fmt:formatDate pattern="yyyy-MM-dd" value="${task.datPlanStart}" /> >
                                <span class="input-group-addon"><i class="ti-calendar"></i></span>
                            </div>
                    </div>
                </div>

                <div class="form-group div-Hidden">
                    <label class="col-md-2 control-label">计划完成时间<i class="fa fa-asterisk txt-color-red"></i></label>
                    <div class="col-md-6">
                        <div class="input-group date">
                            <input type="text" class="form-control" id="datPlanEnd" name="datPlanEnd" placeholder="请选择计划完成时间"
                                   value=<fmt:formatDate pattern="yyyy-MM-dd" value="${task.datPlanEnd}" /> >
                            <span class="input-group-addon"><i class="ti-calendar"></i></span>
                        </div>
                    </div>
                </div>

                <div class="form-group div-Hidden">
                    <label class="col-md-2 control-label">预警时间<i class="fa fa-asterisk txt-color-red"></i></label>
                    <div class="col-md-6">
                        <div class="input-group date">
                            <input type="text" class="form-control" id="datWarning" name="datWarning" placeholder="请选择预警时间"
                                   value=<fmt:formatDate pattern="yyyy-MM-dd" value="${task.datWarning}" /> >
                            <span class="input-group-addon"><i class="ti-calendar"></i></span>
                        </div>
                    </div>
                </div>


                <div class="form-group">
                    <label class="col-md-2 control-label">优先程度<i class="fa fa-asterisk txt-color-red"></i>
                    </label>
                    <div class="col-md-6">

                        <div class="radio">
                            <!-- Inline radio buttons -->
                            <input type="radio" name="priority" id="priority1Selected" class="magic-radio" value="0"
                            <c:if test="${task.priority==0}"> checked="checked"</c:if> >
                            <label for="priority1Selected">一般</label>

                            <input type="radio" name="priority" id="priority2Selected" class="magic-radio" value="1"
                            <c:if test="${task.priority==1}"> checked="checked"</c:if> >
                            <label for="priority2Selected">重要</label>

                            <input type="radio" name="priority" id="priority3Selected" class="magic-radio" value="2"
                            <c:if test="${task.priority==2}"> checked="checked"</c:if> >
                            <label for="priority3Selected">紧急</label>

                        </div>
                    </div>
                </div>

                <c:if test="${task.numStatus==0 || handle=='revise'}">
                <div class="form-group">
                    <label class="col-md-2 control-label">相关附件</label>
                    <div class="col-md-6">
                        <input name="uploadify" id="fileAttachment" placeholder="" type="file" >
                        <input name="hiddenFiles" id="hiddenFilesAttachment" type="hidden" value="${outfile.fileAttachment}">
                    </div>
                </div>
                </c:if>

                <div class="form-group">
                    <label class="col-md-2 control-label">提交人</label>
                    <div class="col-md-2">
                        <p class="form-control-static">${task.creater.name}</p>
                    </div>
                    <label class="col-md-2 control-label">提交日期</label>
                    <div class="col-md-2">
                        <p class="form-control-static"><fmt:formatDate pattern="yyyy-MM-dd" value="${task.dateApply}" /></p>
                    </div>
                </div>
            </form>
            <!--===================================================-->
            <!-- END BASIC FORM ELEMENTS -->

            <div class="panel-footer">
                <a class="btn btn-default" id="apply-re-common" href="javascript:void(0)">
                    <i class="fa fa-lg fa-mail-reply"></i> 返回视图
                </a>
                <c:if test="${task.numStatus==0 && user.id==task.creater.id}">
                    <a class="btn btn-success" id="btn-save-common" href="javascript:void(0);"><i class="fa fa-lg fa-download"></i> 保存表单 </a>
                    <a class="btn btn-warning" id="btn-submit-common" href="javascript:void(0);"><i class="fa fa-upload"></i> 提交表单 </a>
                </c:if>
                <c:if test="${task.numStatus==1}">
                    <a class="btn btn-success" id="btn-update-common" href="javascript:void(0);"><i class="fa fa-lg fa-edit"></i> 完成修改 </a>
                </c:if>
            </div>
        </div>
    </div>
</div>

<script>

    // 日期选择
    $('.input-group.date').datepicker({
        format: 'yyyy-mm-dd',
        weekStart: 1,
        autoclose: true,
        todayBtn: 'linked',
        todayHighlight: true,
        language: 'zh-CN'
    });

    // 字典列表下拉框
    $("#dictItemId").select2({
        placeholder: "请选择一个所属项目",
        allowClear: true
    });

    // 附件上传
    inputLoad({
        objId:"fileAttachment",
        entityName:"fileAttachment",
        sourceId:"hiddenFilesAttachment"
    });

    //判断是否已上传了附件
    function hasFile(file,fileUploaded,msg){
        if(fileUploaded.length==0){
            if(file.length==0){
                __toastr_error(msg);
                return false;
            }
        }
        return true;
    }

    function clearRelateTasks(nameField,idField) {
        $("#"+nameField).val("");
        $("#"+idField).val("");
        __toastr_info("选择的关联任务已被清空！");
    }

    function fn_tasks_dialog_select(nameField,idField) {
        __open_dialog_select('选择关联任务', '<%=path%>/activiti/task/dialogs?sids='+$("#"+idField).val(), function (dialogRef) {
            var callback = function (data) {
                if (data) {
                    dialogRef.close();

                    // 获取 user
                    $('#'+idField).val(data.ids);
                    $('#'+nameField).val(data.names);
                } else {
                    dialogRef.enableButtons();
                }
            };
            fn_dialog_tasks_select(callback);
        });
    }

    function fn_members_dialog_select(nameField,idField) {
        __open_dialog_select('选择成员', '<%=path%>/main/member/dialogs?sids='+$("#"+idField).val(), function (dialogRef) {
            var callback = function (data) {
                if (data) {
                    dialogRef.close();

                    // 获取 user
                    $('#'+idField).val(data.ids);
                    $('#'+nameField).val(data.names);
                } else {
                    dialogRef.enableButtons();
                }
            };
            fn_dialog_members_select(callback);
        });
    }

    //校验
    function infoValidate() {
        var tmp = "";

        tmp = $("select#dictItemId").val();
        if(!tmp){
            __toastr_error("请选择所属项目！");
            return false;
        }

        var keyId = $("input#keyId").val();
        var relateTaskIds = $("input#relateTaskIds").val();
        if(relateTaskIds){
            if (relateTaskIds.indexOf(keyId)>-1) {
                __toastr_error("选择的关联任务不能包括自己，请重新选择！");
                return false;
            }
        }

        tmp = $("input#name").val();
        if(!tmp){
            __toastr_error("请输入任务名称！");
            return false;
        }

        if($('#detail-summernote').summernote('isEmpty')){
            __toastr_error("请输入详细要求！");
            return false;
        } else {
            var sHTML = $('#detail-summernote').summernote("code");
            $("input#detail").val(sHTML);
        }

        tmp = $("input#leaderIds").val();
        if(!tmp){
            __toastr_error("请选择任务责任人！");
            return false;
        }

        tmp = $("input#memberIds").val();
        if(!tmp){
            __toastr_error("请选择任务组员！");
            return false;
        }

        tmp = $("input#datPlanStart").val();
        if(!tmp){
            __toastr_error("请选择计划开始时间！");
            return false;
        }

        tmp = $("input#datPlanEnd").val();
        if(!tmp){
            __toastr_error("请选择计划完成时间！");
            return false;
        }

        tmp = $("input#datWarning").val();
        if(!tmp){
            __toastr_error("请选择预警时间！");
            return false;
        }

        return true;
    }

    //保存
    $("#btn-save-common").click(
            function(e) {
                __confirm_dialog("提示：","确定保存吗？",
                        function () {
                            var sHTML = $('#detail-summernote').summernote("code");
                            $("input#detail").val(sHTML);
                            __form_save("form_task_input", "<%=path%>/activiti/task/save");
                            returnToHashUrl();
                        }
                )
            }
    );

    //提交
    $("#btn-submit-common").click(
            function(e) {
                var $valid = infoValidate();
//                console.log("v:"+$valid);
                if(!$valid) return false;

                __confirm_dialog("提示：","提交后填写资料不得修改，请确认是否进行提交？",
                        function () {
                            __form_save("form_task_input", "<%=path%>/activiti/task/submit");
                            returnToHashUrl();
                        }
                )
            }
    );

    // 完成修改
    $("#btn-update-common").click(
        function(e) {
            var $valid = infoValidate();
            if(!$valid) return false;

            __confirm_dialog("提示：","请确认是否完成修改？",
                function () {
                    __form_save("form_task_input", "<%=path%>/activiti/task/update");
                    returnToHashUrl();

                    // 刷新grid
                    $('#task_running_table').trigger('reloadGrid');
                }
            )
        }
    );

    //返回视图
    $("#apply-re-common").click(returnToHashUrl);

    // 加载完成后执行
    $(function(){
        $('#detail-summernote').summernote({
            lang : 'zh-CN',
            height : '230px'
        });
        //赋值
        var sHtml = $("#detail").val();
        if(sHtml.length>0){
            $('#detail-summernote').summernote("code",sHtml);
        }

    })

</script>




